package deployment

import (
	"k8sapi/core"
	"k8sapi/lib"
)
//这里的函数好比 DTO . 把原生的 deployment 或pod转换为  自己的 实体对象
func ListPodsByLabel(ns string,labels []map[string]string) (ret []*Pod){
   list,err :=core.PodMap.ListByLabels(ns,labels)
   lib.CheckError(err)
	for _,item:=range list{
		ret=append(ret,&Pod{
			Name:item.Name,
			Images:GetImagesByPod(item.Spec.Containers),
			Namespace: item.Namespace,
			IP:[]string{item.Status.PodIP,item.Status.HostIP},
			Phase: string(item.Status.Phase),
			NodeName:item.Spec.NodeName,
			Message: core.EventMap.GetMessage(item.Namespace,"Pod",item.Name),
			CreateTime:item.CreationTimestamp.Format("2006-01-02 15:04:05"),
			IsReady: GetPodisReady(item),
		})
	}
	return
}
//显示 所有 deployments （根据namespace)
func ListAll(namespace string ) (ret []*Deployment){
	depList,err:=core.DepMap.ListByNS(namespace)
	lib.CheckError(err)
	for _,item:=range depList{ //遍历所有deployment
	// 加入副本数
		ret=append(ret,&Deployment{Name:item.Name,
				Replicas:[3]int32{item.Status.Replicas,item.Status.AvailableReplicas,item.Status.UnavailableReplicas},
				Images:GetImages(*item),
				IsComplete: GetDeploymentIsComplete(item),
				Message:GetDeploymentCondition(item),
		})
	}
	return
}